package com.csun.nusing.ui.myview;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.csun.nusing.R;

import java.lang.reflect.Field;

/**
 * A simple dialog containing an {@link DatePicker}.
 * 
 * <p>
 * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
 * guide.
 * </p>
 */
public class DoubleTimerPickerDialog extends AlertDialog implements
		OnClickListener, OnTimeChangedListener {

	private static final String START_HOUR = "start_hour";
	private static final String END_HOUR = "end_hour";
	private static final String START_MIN = "start_min";
	private static final String END_MIN = "end_min";

	private final TimePicker mDatePicker_start;
	private final TimePicker mDatePicker_end;
	private final OnDateSetListener mCallBack;

	/**
	 * The callback used to indicate the user is done filling in the date.
	 */
	public interface OnDateSetListener {

		/**
		 * @param view
		 *            The view associated with this listener.
		 * @param year
		 *            The year that was set.
		 * @param monthOfYear
		 *            The month that was set (0-11) for compatibility with
		 *            {@link java.util.Calendar}.
		 * @param dayOfMonth
		 *            The day of the month that was set.
		 */
		void onDateSet(TimePicker startDatePicker, int starthour, int startMin,
					   TimePicker endDatePicker, int endhour, int endMin);
	}

	/**
	 * @param context
	 *            The context the dialog is to run in.
	 * @param callBack
	 *            How the parent is notified that the date is set.
	 * @param year
	 *            The initial year of the dialog.
	 * @param monthOfYear
	 *            The initial month of the dialog.
	 * @param dayOfMonth
	 *            The initial day of the dialog.
	 */
	public DoubleTimerPickerDialog(Context context, OnDateSetListener callBack,
			int hour, int min, int endhour, int endMin) {
		this(context, 0, callBack, hour, min,endhour,endMin);
	}

	public DoubleTimerPickerDialog(Context context, int theme,
			OnDateSetListener callBack, int hour, int min, int endhour, int endMin) {
		this(context, 0, callBack, hour, min,endhour,endMin, true);
	}

	/**
	 * @param context
	 *            The context the dialog is to run in.
	 * @param theme
	 *            the theme to apply to this dialog
	 * @param callBack
	 *            How the parent is notified that the date is set.
	 * @param year
	 *            The initial year of the dialog.
	 * @param monthOfYear
	 *            The initial month of the dialog.
	 * @param dayOfMonth
	 *            The initial day of the dialog.
	 */
	public DoubleTimerPickerDialog(Context context, int theme,
			OnDateSetListener callBack, int hour, int min, int endhour, int endMin, boolean isDayVisible) {
		super(context, theme);

		mCallBack = callBack;

		Context themeContext = getContext();
		setButton(BUTTON_POSITIVE, "确 定", this);
		setButton(BUTTON_NEGATIVE, "取 消", this);
		// setButton(BUTTON_POSITIVE,
		// themeContext.getText(android.R.string.date_time_done), this);
		setIcon(0);

		LayoutInflater inflater = (LayoutInflater) themeContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.date_picker_dialog, null);
		
		setView(view);
		mDatePicker_start = view
				.findViewById(R.id.datePickerStart);
		mDatePicker_end = view.findViewById(R.id.datePickerEnd);
		hidDay(mDatePicker_start);
		hidDay(mDatePicker_end);
		mDatePicker_start.setIs24HourView(true);
		mDatePicker_end.setIs24HourView(true);
	}

	public void onClick(DialogInterface dialog, int which) {
		// Log.d(this.getClass().getSimpleName(), String.format("which:%d",
		// which));
		// 如果是“取 消”按钮，则返回，如果是“确 定”按钮，则往下执行
		if (which == BUTTON_POSITIVE)
			tryNotifyDateSet();
	}

	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
		if (view.getId() == R.id.datePickerStart) {
			mDatePicker_start.setCurrentHour(hourOfDay);
			mDatePicker_start.setCurrentMinute(minute);
		}
		if (view.getId() == R.id.datePickerEnd) {
			mDatePicker_end.setCurrentHour(hourOfDay);
			mDatePicker_end.setCurrentMinute(minute);
		}
	}

	private void hidDay(TimePicker timePicker2) {
		Field[] datePickerfFields = timePicker2.getClass().getDeclaredFields();
		for (Field datePickerField : datePickerfFields) {
			if ("mMinuteSpinner".equals(datePickerField.getName())) {
				// 这里这个变量是系统的定义的变量，不是自己定义的​
				datePickerField.setAccessible(true);
				Object dayPicker = new Object();
				try {
					dayPicker = datePickerField.get(timePicker2);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				}
				// datePicker.getCalendarView().setVisibility(View.GONE);
				((View) dayPicker).setVisibility(View.GONE);
			}
		}
	}

	/**
	 * 获得开始日期的DatePicker
	 * 
	 * @return The calendar view.
	 */
	public TimePicker getDatePickerStart() {
		return mDatePicker_start;
	}

	/**
	 * 获得结束日期的DatePicker
	 * 
	 * @return The calendar view.
	 */
	public TimePicker getDatePickerEnd() {
		return mDatePicker_end;
	}

	/**
	 * Sets the start date.
	 * 
	 * @param year
	 *            The date year.
	 * @param monthOfYear
	 *            The date month.
	 * @param dayOfMonth
	 *            The date day of month.
	 */
	public void updateStartDate(int hour, int min) {
		mDatePicker_start.setCurrentHour(hour);
		mDatePicker_start.setCurrentMinute(min);
	}

	/**
	 * Sets the end date.
	 * 
	 * @param year
	 *            The date year.
	 * @param monthOfYear
	 *            The date month.
	 * @param dayOfMonth
	 *            The date day of month.
	 */
	public void updateEndDate(int hour, int min) {
		mDatePicker_end.setCurrentHour(hour);
		mDatePicker_end.setCurrentMinute(min);
	}

	private void tryNotifyDateSet() {
		if (mCallBack != null) {
			mDatePicker_start.clearFocus();
			mDatePicker_end.clearFocus();
			mCallBack.onDateSet(mDatePicker_start,
					mDatePicker_start.getCurrentHour(),
					mDatePicker_start.getCurrentMinute(), mDatePicker_end,
					mDatePicker_end.getCurrentHour(),
					mDatePicker_end.getCurrentMinute());
		}
	}

	@Override
	protected void onStop() {
		// tryNotifyDateSet();
		super.onStop();
	}

	@Override
	public Bundle onSaveInstanceState() {
		Bundle state = super.onSaveInstanceState();
		state.putInt(START_HOUR, mDatePicker_start.getCurrentHour());
		state.putInt(START_MIN, mDatePicker_start.getCurrentMinute());
		state.putInt(END_HOUR, mDatePicker_end.getCurrentHour());
		state.putInt(END_MIN, mDatePicker_end.getCurrentMinute());
		return state;
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		int start_year = savedInstanceState.getInt(START_HOUR);
		int start_month = savedInstanceState.getInt(START_MIN);
		mDatePicker_start.setCurrentHour(start_year);
		mDatePicker_start.setCurrentMinute(start_month);
		int end_year = savedInstanceState.getInt(END_HOUR);
		int end_month = savedInstanceState.getInt(END_MIN);
		mDatePicker_end.setCurrentHour(end_year);
		mDatePicker_end.setCurrentMinute(end_month);

	}

}